/**
 * Simple Line Chart View Working.
 *
 * Released under Apache License, Version 2.0.
 */
 
package bsp.eclair.simplelinechart;

import java.util.Calendar;
import java.util.Vector;

import bsp.eclair.simplelinechart.viewworld.Point2DFloat;
import bsp.eclair.simplelinechart.viewworld.Point2DInt;
import bsp.eclair.simplelinechart.viewworld.RectFloat;
import bsp.eclair.simplelinechart.viewworld.RectInt;
import bsp.eclair.simplelinechart.viewworld.ViewWorld;
import bsp.eclair.simplelinechart.viewworld.ViewWorldWorking;
import bsp.eclair.simplelinechart.viewworld.ViewWorldQuadrant1;
import bsp.eclair.simplelinechart.viewworld.ViewWorldQuadrant4;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;

@SuppressWarnings("unused")
public class SimpleLineChartViewWorking
	extends SurfaceView
	implements SurfaceHolder.Callback
{
	protected ActivityMain mParentActivity;
	protected int mWidth, mHeight, mOriginX, mOriginY;
	protected ViewWorldWorking mViewWorld;
	protected Vector<ChartPoint> mData;

	public SimpleLineChartViewWorking(Context context)
	{
		super(context);
		
		initialize(context);
	}
	
	public SimpleLineChartViewWorking(Context context, AttributeSet set)
	{
		super(context, set);
		
		initialize(context);
	}
	
	@Override
	public void surfaceCreated(SurfaceHolder holder)
	{
		mWidth = getWidth();
		mHeight = getHeight();
		
		mViewWorld = new ViewWorldWorking(0f, 0f, 1f, 1f, 0.0f, 0.0f, 0, 0, mWidth, mHeight, 5, 5, 5, 5);
		
		mData = new Vector<ChartPoint>();
		//Log.d("SimpleLineChartViewWorking", "adding point at 0,0");
		mData.add(new ChartPoint(0, 0, false));
		
		setWillNotDraw(false);
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
	{
	}
 
	@Override
	public void surfaceDestroyed(SurfaceHolder holder)
	{
	}
	
	@Override
	public void onDraw(Canvas canvas)
	{
		int sx, sy, ex, ey;
		Paint paintView, paintWorld, paintTouch;
		RectFloat worldDimensions;
		RectInt viewDimensions;
		Point2DFloat worldOrigin;
		Point2DInt viewOrigin;
		ChartPoint wpt1, wpt2;
		Point2DInt vpt1, vpt2;
		
		if ( canvas == null ) {
			return;
		}
		
		if ( mViewWorld == null ) {
			return;
		}
		
		canvas.drawColor(Color.BLACK);
		
		paintView = new Paint();
		paintView.setColor(Color.YELLOW);
		
		paintWorld = new Paint();
		paintWorld.setColor(Color.GREEN);
		
		paintTouch = new Paint();
		paintTouch.setColor(Color.RED);
		
		worldDimensions = mViewWorld.getWorldDimensions();
		viewDimensions = mViewWorld.getViewDimensions();
		worldOrigin = mViewWorld.getWorldOrigin();
		
		/**/
		//
		// draw the data as per display - Quadrant 4 - 0,0 in top left corner
		//
		
		viewOrigin = mViewWorld.convertToViewQuadrant4(worldOrigin);

		// draw the origin
		canvas.drawCircle(viewOrigin.mX, viewOrigin.mY, 4, paintView);
		
		// draw the x-axis and y-axis
		// draw -ve x-axis
		canvas.drawLine(viewOrigin.mX, viewOrigin.mY, viewDimensions.left, viewOrigin.mY, paintView);
		// draw +ve x-axis
		canvas.drawLine(viewOrigin.mX, viewOrigin.mY, viewDimensions.right, viewOrigin.mY, paintView);
		// draw -ve y-axis
		canvas.drawLine(viewOrigin.mX, viewOrigin.mY, viewOrigin.mX, viewDimensions.top, paintView);
		// draw +ve y-axis
		canvas.drawLine(viewOrigin.mX, viewOrigin.mY, viewOrigin.mX, viewDimensions.bottom, paintView);
		
		// draw the data lines
		for ( int i = 1; i < mData.size(); i++ ) {
			wpt1 = mData.get(i-1);
			wpt2 = mData.get(i);
			
			vpt1 = mViewWorld.convertToViewQuadrant4(wpt1.point);
			vpt2 = mViewWorld.convertToViewQuadrant4(wpt2.point);
			
			canvas.drawLine(vpt1.mX, vpt1.mY, vpt2.mX, vpt2.mY, paintView);
		}
		
		// draw the points
		for ( int i = 0; i < mData.size(); i++ ) {
			wpt1 = mData.get(i);
			vpt1 = mViewWorld.convertToViewQuadrant4(wpt1.point);
			if ( wpt1.selectionFlag == false ) {
				canvas.drawCircle(vpt1.mX, vpt1.mY, 4, paintView);
			} else {
				canvas.drawCircle(vpt1.mX, vpt1.mY, 4, paintTouch);
			}
		}
		/**/
		
		/**/
		//
		// draw the data as per world - Quadrant 1 - 0,0 in bottom left corner
		//
		
		viewOrigin = mViewWorld.convertToViewQuadrant1(worldOrigin);
		
		// draw the origin
		canvas.drawCircle(viewOrigin.mX, viewOrigin.mY, 4, paintWorld);
		
		// draw the x-axis and y-axis
		// draw -ve x-axis
		canvas.drawLine(viewOrigin.mX, viewOrigin.mY, viewDimensions.left, viewOrigin.mY, paintWorld);
		// draw +ve x-axis
		canvas.drawLine(viewOrigin.mX, viewOrigin.mY, viewDimensions.right, viewOrigin.mY, paintWorld);
		// draw -ve y-axis
		canvas.drawLine(viewOrigin.mX, viewOrigin.mY, viewOrigin.mX, viewDimensions.top, paintWorld);
		// draw +ve y-axis
		canvas.drawLine(viewOrigin.mX, viewOrigin.mY, viewOrigin.mX, viewDimensions.bottom, paintWorld);
		
		// draw the lines
		for ( int i = 1; i < mData.size(); i++ ) {
			wpt1 = mData.get(i-1);
			wpt2 = mData.get(i);
			
			vpt1 = mViewWorld.convertToViewQuadrant1(wpt1.point);
			vpt2 = mViewWorld.convertToViewQuadrant1(wpt2.point);
			
			canvas.drawLine(vpt1.mX, vpt1.mY, vpt2.mX, vpt2.mY, paintWorld);
		}
		
		// draw the points
		for ( int i = 0; i < mData.size(); i++ ) {
			wpt1 = mData.get(i);
			vpt1 = mViewWorld.convertToViewQuadrant1(wpt1.point);
			if ( wpt1.selectionFlag == false ) {
				canvas.drawCircle(vpt1.mX, vpt1.mY, 4, paintWorld);
			} else {
				canvas.drawCircle(vpt1.mX, vpt1.mY, 4, paintTouch);
			}
		}
		/**/
	}
	
	protected boolean isInside(float x1, float y1, float x2, float y2)
	{
		boolean retVal = false;
		
		if ( x2 >= (x1 - 2) && x2 <= ( x1 + 2 ) && y2 >= (y1 - 2) && y2 <= ( y1 + 2 ) ) {
			retVal = true;
		}
		
		return retVal;
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event)
	{
		if ( event.getAction() == MotionEvent.ACTION_DOWN ) {
			int x = (int)event.getX();
			int y = (int)event.getY();
			
			Point2DFloat pt = mViewWorld.convertToWorldQuadrant1(x, y);
			ChartPoint cpt1;
			
			for ( int i = 0; i < mData.size(); i++ ) {
				cpt1 = mData.get(i);
				if ( isInside(cpt1.point.mX, cpt1.point.mY, pt.mX, pt.mY) ) {
					cpt1.selectionFlag = true;
				}
			}
		}
		
		return true;
	}
	
	protected void initialize(Context context)
	{
		getHolder().addCallback(this);
		setFocusable(true);
	}
	
	public void setParentActivity(ActivityMain parent)
	{
		mParentActivity = parent;
	}
	
	public ActivityMain getParentActivity()
	{
		return mParentActivity;
	}
	
	/*
	public ViewWorldPrev getViewWorld()
	{
		return mViewWorld;
	}
	*/
	
	public ViewWorldWorking getViewWorld()
	{
		return mViewWorld;
	}
	
	public void addData(float x, float y)
	{
		//Log.d("SimpleLineChartViewWorking", "adding point at " + x + "," + y);
		mData.add(new ChartPoint(x, y, false));
	}
}

/* end */
